package com.estronger.shopmanage.fragment.measure;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.pull_to_refresh_expandablelist.CommonFooterView.State;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase.Mode;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase.OnRefreshListener;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshListView;
import com.estronger.shopmanage.fragment.FragmentFactory;
import com.estronger.shopmanage.ui.measure.MeasureActivity;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.MeasureAdapter;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B10MeasureList;
import com.estronger.shopmanage.bean.B10MeasureList.MeasureListData;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.order.activity.OrderQueryActivity;
import com.estronger.shopmanage.ui.order.until.Tools;
import com.estronger.shopmanage.utils.NetUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;
import com.estronger.shopmanage.view.CircularProgress;
import com.estronger.shopmanage.view.SelectPopupWindow;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;

/**
 * 测量的五个Fregment的公共父类
 * 
 * @author Administrator
 * 
 */
public abstract class MeasureBaseFragment extends Fragment implements
		OnRefreshListener<ListView> {

	protected PullToRefreshListView mListView;// 下拉刷新的ListView
	protected RequestParams params;
	protected RelativeLayout root;// 根View
	protected CircularProgress bar;// 进度条
	protected TextView empty_view;// 没数据是展示
	protected MeasureAdapter adapter;// 适配器
	private RelativeLayout searchLayout;
	private EditText etfInputSea;
	private TextView startDateText;
	private TextView endDateText;
	private ImageView searchBtn;
	RelativeLayout dateSelect;
	RelativeLayout datebox;
	TextView dateboxtxt;

	SelectPopupWindow sPopWindow;

	// 存放测量条目数据
	protected ArrayList<MeasureListData> data = new ArrayList<MeasureListData>();
	Gson gson = new Gson();
	private MeasureActivity activity;

	protected NetManager netManager = new NetManager() {
		@Override
		public void onNetWorkErorr(int code, String result) {
			if (null != mListView) {
				mListView.setState(State.HIDE);
				if (null != bar)
					bar.setVisibility(View.GONE);
				mListView.onRefreshComplete();
				if (data.size() == 0) {
					empty_view.setVisibility(View.VISIBLE);
				}
			}
		}

		@Override
		public void onNetWorkSuccess(int code, String result) {
			if (null != bar)
				bar.setVisibility(View.GONE);
			if (null != mListView) {
				mListView.setState(State.HIDE);
				mListView.onRefreshComplete();
				if (code == NetManager.MEASURE_LIST) {
					B10MeasureList bean = gson.fromJson(result,
							B10MeasureList.class);
					if (bean.success && bean.data != null) {

						data.clear();
						data.addAll(bean.data);
						adapter.notifyDataSetChanged();
						if (data.size() > 0
								&& empty_view.getVisibility() == View.VISIBLE) {
							empty_view.setVisibility(View.GONE);
						}
						if (data.size() == 0) {
							empty_view.setVisibility(View.VISIBLE);
						}
					} else {
						empty_view.setVisibility(View.VISIBLE);
					}
				}
			}
		}
	};

	public void connect(final String gettype) {

		if(activity==null){
			activity = (MeasureActivity) getActivity();
		}else {
			activity.getTitleNum();
		}
		if (bar != null){
			bar.setVisibility(View.VISIBLE);
		}
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				String searchText = etfInputSea.getText().toString().trim();
				String startTime = startDateText.getText().toString();
				String endTime = endDateText.getText().toString();
				params = new RequestParams();
				params.addBodyParameter("usercode", MyApplication.getUserCode());
				params.addBodyParameter("gettype", gettype);
				params.addBodyParameter("cstname",searchText);
				params.addBodyParameter("pmdatestart",startTime);
				params.addBodyParameter("pmdateend",endTime);
				params.addBodyParameter("sf_action", "getworklist");
				netManager.connectWork(params, NetManager.MEASURE_LIST);
			}
		}, 1000);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// return super.onCreateView(inflater, container, savedInstanceState);
		activity = (MeasureActivity) getActivity();
		root = (RelativeLayout) View.inflate(activity,
				R.layout.fragment_history, null);
		mListView = (PullToRefreshListView) root
				.findViewById(R.id.success_listview);
		searchLayout = root.findViewById(R.id.search_rl);
		bar = (CircularProgress) root.findViewById(R.id.loding_view);
		empty_view = (TextView) root.findViewById(R.id.empty_view);

		/*过滤*/
		startDateText = root.findViewById(R.id.start_time);
		endDateText = root.findViewById(R.id.end_time);
		searchBtn = root.findViewById(R.id.searc_btn);
		datebox = root.findViewById(R.id.datebox);
		dateboxtxt = root.findViewById(R.id.dateboxtxt);
		dateSelect = root.findViewById(R.id.dateSelect);
		//bar上的选择全部还是审批日期条件
		datebox.setOnClickListener(yishenListener);

		startDateText.setOnClickListener(onClickListener);
		endDateText.setOnClickListener(onClickListener);
		searchBtn.setOnClickListener(onClickListener);
		// mListView.setOnScrollListener(this);
		mListView.setOnRefreshListener(this);
		mListView.setMode(Mode.PULL_FROM_START);
		mListView.setState(State.HIDE);

		boolean isAll = "4".equals(gettype());
		//		全部的时候显示搜索框
		searchLayout.setVisibility(isAll?View.VISIBLE:View.GONE);
		etfInputSea =  root.findViewById(R.id.etfInputSea);

		adapter = new MeasureAdapter(activity, data, gson, isAll);
		mListView.setAdapter(adapter);

		etfInputSea.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER){
					InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(etfInputSea,InputMethodManager.SHOW_FORCED);
					imm.hideSoftInputFromWindow(etfInputSea.getWindowToken(), 0);
					searchForKey();
				}
				return false;
			}
		});

		return root;
	}

	private View.OnClickListener yishenListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.datebox:
					changeDateboxDrawable(0);
					sPopWindow = new SelectPopupWindow(activity,
							dateboxtxt.getText().toString(), activity.getWindow(), true, searchLayout);
					sPopWindow.showPopupWindow(datebox);
					sPopWindow.showTextView(dateboxtxt, etfInputSea, dateSelect);
					break;
			}
		}
	};



	public void changeDateboxDrawable(int ud) {
		Drawable drawable = null;
		switch (ud) {
			case 0:
				break;
			case 1:

				break;
		}
	}

	protected abstract boolean needConnect();

	protected abstract String gettype();

	protected void loadData() {
		if (!NetUtils.isNetworkConnected(activity)) {
			mListView.postDelayed(new Runnable() {
				@Override
				public void run() {
					mListView.onRefreshComplete();
					Toast.show(activity, "当前无网络链接！", 2000);
				}
			}, 100);
		} else {
			connect(gettype());
		}
	}

	/**
	 * 下拉刷新时调用
	 */
	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadData();
	}

	View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			String startStr = startDateText.getText().toString();
			String endTime = endDateText.getText().toString();
			if (v.getId() == R.id.start_time) {
				AppDialog.createPickerTJstart(activity, startDateText, startStr,"", endTime).show();
			}else if (v.getId() == R.id.end_time){
				AppDialog.createPickerTJstart(activity, endDateText, endTime,startStr,"").show();
			}else if (v.getId() == R.id.searc_btn){
				searchForKey();
			}
		}
	};

	/*
	* 关键字查询
	* */
	public void searchForKey(){
		loadData();
	}
}
